//
//  ZQEditAmindPage.m
//  jianzhik
//
//  Created by huami on 2018/8/28.
//  Copyright © 2018年 WKWebView. All rights reserved.
//
#import "UIView+zq_extension.h"
#import "ZQEditAmindPage.h"
#import "DOPDropDownMenu.h"
#import "ZQDisplayButton.h"
#import "ZQDisplayButtonView.h"
#import "ZQListTableViewCell.h"
static NSString * const kCellID = @"cell";
@interface ZQEditAmindPage ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UIView *menuView;
@property (weak, nonatomic) IBOutlet UILabel *loginNameLabel;
@property (weak, nonatomic) IBOutlet UITextField *psdTextField;

@property (nonatomic, strong) NSMutableArray * boolArr;
/** headLabel */
@property (nonatomic, weak) UILabel *headLabel;
/** menu */
@property (nonatomic, copy) NSString *menuStr;
/** ivButton */
@property (nonatomic, weak) UIButton *ivButtoon;

/** tableView */
@property (nonatomic, strong) UITableView *tableView;
/** headViewButton */
@property (nonatomic, weak) ZQDisplayButtonView *headViewButton;

@end

@implementation ZQEditAmindPage
- (ZQDisplayButtonView *)headViewButton
{
    if (!_headViewButton) {

        ZQDisplayButtonView *headViewButton = [ZQDisplayButtonView ZQ_ViewFromXib];
        _headViewButton = headViewButton;
    }
    return _headViewButton;
}
- (IBAction)changeBtnClick:(UIButton *)sender {
    if (self.psdTextField.enabled) {
        self.psdTextField.enabled = NO;

        [sender setTitle:@"修改" forState:UIControlStateNormal];
    
    }else{
         self.psdTextField.enabled = YES;
         self.psdTextField.text = @"";
        
         [sender setTitle:@"完成" forState:UIControlStateNormal];
    }
   
}

- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([ZQListTableViewCell class]) bundle:nil] forCellReuseIdentifier:kCellID];
        _tableView.tableFooterView = [[UIView alloc] init];
    }
    return _tableView;
}


- (NSMutableArray *)boolArr {
    
    if (!_boolArr) {
        
        _boolArr = [NSMutableArray arrayWithObjects:@"NO", nil];
    }
    return _boolArr;
}


- (NSString *)menuStr
{
    if (!_menuStr) {
        _menuStr = @"正常";
    }
    return _menuStr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.psdTextField.enabled = NO;
    [self.view addSubview:self.tableView];
    [self.footButton setTitle:@"保存设置" forState:UIControlStateNormal];
    [self.footButton addTarget:self action:@selector(pop) forControlEvents:UIControlEventTouchUpInside];
    self.psdTextField.placeholder = @"请输入修改密码";
    self.loginNameLabel.text = self.editModels.Username;

    self.psdTextField.text= @"************";
    self.psdTextField.secureTextEntry = YES;
}

- (void)pop
{
    if (self.psdTextField.text.length == 0) {
        [UIHelper showTipsWithText:@"密码不能为空" inView:self.view];
        return;
    }
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"Id"] = @(self.editModels.Id);
    parameters[@"Status"] = [self.menuStr isEqualToString:@"正常"] ? @(10) : @(20) ;
    parameters[@"Password"] = self.psdTextField.text;
    parameters[@"Remarks"] = @"";

    [ZQAFHelper afnetworkRequestWithPostURL:@"/v1/user/updateuserbyid" parameters:parameters view:self.view finishedBlcok:^(NSDictionary *dict) {
        [UIHelper showSuccessMessage:dict[@"Msg"] afterHideHud:2 toView:self.view completion:^{
            [self.navigationController popViewControllerAnimated:YES];
        }];
    }];
}

- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    self.tableView.frame = self.menuView.frame;
    self.ivButtoon.frame = self.menuView.frame;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    
    return self.menuView.height / 2;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return self.menuView.height / 2;
}

#pragma mark - tableView delegate and dataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if ([self.boolArr[0] boolValue] == NO) {
        
        return 0;
    }else {
        
        return 1;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
//        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    
    ZQListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellID  forIndexPath:indexPath];
    NSString *str = [self.menuStr isEqualToString:@"正常"] ? @"禁用":@"正常";
//    cell.textLabel.text = str;
    [cell.listButton setTitle:str forState:UIControlStateNormal];
 
    NSLog(@"cell.listButton%@",[cell.listButton currentTitle]);
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.menuStr = [self.menuStr isEqualToString:@"正常"] ? @"禁用":@"正常";
    
   [self.boolArr replaceObjectAtIndex:0 withObject:@NO];
    
    [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationFade];

}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    //创建header的view


    UIImage *currentImg = [_boolArr[section] boolValue] ? [UIImage imageNamed:@"buddy_header_arrow_down"] : [UIImage imageNamed:@"buddy_header_arrow_right"];
//    [ivButton sizeToFit];
    [self.headViewButton.headViewButton setImage:currentImg forState:UIControlStateNormal];
    [self.headViewButton.headViewButton setTitleColor:[UIColor stringToColor:@"#1890ff"] forState:UIControlStateNormal];
    [self.headViewButton.headViewButton setTitle:self.menuStr forState:UIControlStateNormal];
 

    
    //添加轻扣手势
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGR:)];
    [self.headViewButton.headViewButton addGestureRecognizer:tap];
    
    return self.headViewButton;
}


#pragma mark - action
- (void)tapGR:(UITapGestureRecognizer *)tapGR {

    //判断改变bool值
    if ([_boolArr[0] boolValue] == YES) {
        [_boolArr replaceObjectAtIndex:0 withObject:@NO];
    }else {
        [_boolArr replaceObjectAtIndex:0 withObject:@YES];
    }
    //刷新某个section
    [_tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationFade];
}
@end
